import { _decorator, Component, Node, EditBox, ToggleContainer, Label } from "cc";
import { LoginController } from "../login/LoginController";
import proto from "../../../../proto_js/proto.js";
import { Session } from "../login/LoginModel";
const { ccclass, property } = _decorator;

@ccclass("LobbyView")
export class LobbyView extends Component {
	@property(Node)
	public popPanel!: Node;

	@property(Label)
	public nameLabel!: Label;

	@property(Label)
	public idLabel!: Label;

	start() {}

	update(deltaTime: number) {}

	public onClickModify() {
		console.log("点击修改信息");
		this.popPanel.active = true;
	}
	public onClickClose() {
		console.log("点击关闭");
		this.popPanel.active = false;
	}

	public onClickChat() {
		console.log("点击聊天");
		LoginController.TestChat();
	}

	public onSureModify() {
		console.log("点击确定修改");
		//
		//获取输入框的值
		const nickname: string = this.popPanel.getChildByName("NickName")?.getComponent(EditBox)?.string || "";
		const avatar: string = this.popPanel.getChildByName("Avatar")?.getComponent(EditBox)?.string || "";
		//获取单选框的值
		const choose = this.popPanel.getChildByName("Gender")?.getComponent(ToggleContainer)?.toggleItems;
		if (!choose) {
			return;
		}
		let idx = 0;
		for (let i = 0; i < choose.length; i++) {
			if (choose[i].isChecked) {
				idx = i;
				break;
			}
		}

		const genderArr = [proto.common.Gender.Male, proto.common.Gender.Female, proto.common.Gender.Unknow];
		//根据下标获取性别

		const gender: number = genderArr[idx];

		LoginController.OnModifyUserInfo(nickname, avatar, gender);
	}

	public onFreshUserInfo() {
		console.log("点击刷新用户信息");
		// LoginController.GetUserInfo();
		this.nameLabel.string = `昵称：${Session.PlayerData.NickName}`;
		this.idLabel.string = `ID：${Session.PlayerData.Guid}`;
		this.popPanel.active = false;
	}
}
